package com.cropdoctor.controller;

import com.cropdoctor.common.Result;
import com.cropdoctor.config.DeepSeekClient;
import com.cropdoctor.model.dto.HistoryImgClsDto;
import com.cropdoctor.model.dto.ImgClassificationDto;
import com.cropdoctor.service.ImgIdfClassificationService;
import com.cropdoctor.util.ContextGet;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "识别果蔬分类控制类")
@RestController
@RequestMapping("/class")
public class ImageIdfClassificationController {

    @Autowired
    ImgIdfClassificationService imgIdfClassificationService;

    @Autowired
    private DeepSeekClient deepSeekClient;

    @PostMapping("/upload")
    @ApiOperation("果蔬分类识别")
    @ResponseBody
    public Result<List<ImgClassificationDto>> upload(@RequestParam("file") MultipartFile img) throws IOException {
        return imgIdfClassificationService.ImgIdf(img);
    }

    @GetMapping("/history")
    @ApiOperation("查询用户识别历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dateTime",value = "时间(传入后可查询当前时间之前)")
    })
    public Result<List<HistoryImgClsDto>> getHistory(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "dateTime",required = false) LocalDateTime dateTime,
                                                    @RequestParam(value = "id",required = false,defaultValue = "0") Long id){
        Long userId = ContextGet.getUserId();
        return imgIdfClassificationService.getHistory(userId, dateTime,id);
    }

    @PostMapping(value = "/deepseek/stream")
    public Result<String> askDeepSeek(@RequestBody String prompt) {
        try {
            return deepSeekClient.callDeepSeek(prompt);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("Error calling DeepSeek API");
        }
    }
}
